/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.web.controller;

import com.wallellen.core.page.ViewMode;
import com.wallellen.knowledge.doc.domain.Doc;
import com.wallellen.knowledge.doc.domain.DocType;
import com.wallellen.knowledge.doc.domain.ex.DocEntire;
import com.wallellen.knowledge.doc.domain.ex.TypeBrief;
import com.wallellen.knowledge.doc.server.DocManagerInter;
import com.wallellen.knowledge.doc.server.DocOperateRightInter;
import com.wallellen.knowledge.doc.server.DocRunInfoInter;
import com.wallellen.knowledge.doc.server.DocTypeInter;
import com.wallellen.knowledge.doc.server.DocgroupManagerInter;
import com.wallellen.knowledge.doc.server.DocmessageManagerInter;
import com.wallellen.knowledge.doc.server.FileManagerInter;
import com.wallellen.knowledge.know.service.KnowServiceInter;
import com.wallellen.knowledge.web.util.ThemesUtil;
import com.wallellen.web.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * 文件
 *
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 */
@RequestMapping("/know")
@Controller
public class KnowController extends WebUtils {
    @Resource
    private DocgroupManagerInter farmDocgroupManagerImpl;
    @Resource
    private FileManagerInter farmFileManagerImpl;
    @Resource
    private DocManagerInter farmDocManagerImpl;
    @Resource
    private DocRunInfoInter farmDocRunInfoImpl;
    @Resource
    private KnowServiceInter KnowServiceImpl;
    @Resource
    private DocmessageManagerInter farmDocmessageManagerImpl;
    @Resource
    private DocOperateRightInter farmDocOperateRightImpl;
    @Resource
    private DocTypeInter farmDocTypeManagerImpl;

    /**
     * 进入创建知识
     *
     * @return
     */
    @RequestMapping("/add")
    public ModelAndView add(String typeid, String groupid, HttpSession session) {
        List<TypeBrief> types = farmDocTypeManagerImpl.getPubTypesForWriteDoc(getCurrentUser(session));

        // 回显分类
        DocEntire doce = new DocEntire();
        DocType doctype = farmDocTypeManagerImpl.getType(typeid);
        doce.setType(doctype);

        // 回显小组
        Doc doc = new Doc();
        doc.setDocgroupid(groupid);
        doce.setDoc(doc);

        return ViewMode.getInstance().putAttr("types", types).putAttr("doce", doce)
                .returnModelAndView(ThemesUtil.getThemePath() + "/know/creat");
    }

    /**
     * 进入网页输入页面
     *
     * @return
     */
    @RequestMapping("/webdown")
    public ModelAndView downWeb(String typeid, String groupid, HttpSession session, HttpServletRequest request) {
        return ViewMode.getInstance().putAttr("typeid", typeid).putAttr("groupid", groupid)
                .returnModelAndView(ThemesUtil.getThemePath() + "/know/downWeb");
    }

    /**
     * 提交并下载网页创建知识
     *
     * @return
     */
    @RequestMapping("/webDLoadDo")
    public ModelAndView downWebCommit(String url, String typeid, String groupid, HttpSession session,
                                      HttpServletRequest request) {
        DocEntire doc = KnowServiceImpl.getDocByWeb(url, getCurrentUser(session));
        if (typeid != null && !typeid.toUpperCase().trim().equals("NONE") && !typeid.toUpperCase().trim().equals("")) {
            DocType doctype = farmDocTypeManagerImpl.getType(typeid);
            doc.setType(doctype);
        }
        if (groupid != null && !groupid.toUpperCase().trim().equals("NONE")
                && !groupid.toUpperCase().trim().equals("")) {
            doc.getDoc().setDocgroupid(groupid);
        }

        List<TypeBrief> types = farmDocTypeManagerImpl.getPubTypesForWriteDoc(getCurrentUser(session));
        return ViewMode.getInstance().putAttr("doce", doc).putAttr("types", types)
                .returnModelAndView(ThemesUtil.getThemePath() + "/know/creat");
    }

    /**
     * 进入修改知识
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    @RequestMapping("/edit")
    public ModelAndView edit(String docid, HttpSession session) {
        DocEntire doce = null;
        try {
            doce = farmDocManagerImpl.getDoc(docid);
            // 解决kindedit中HTML脚本被转义的问题
            doce.getTexts()
                    .setText1(doce.getTexts().getText1().replaceAll("&gt;", "&amp;gt;").replaceAll("&lt;", "&amp;lt;"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        List<TypeBrief> types = farmDocTypeManagerImpl.getPubTypesForWriteDoc(getCurrentUser(session));
        return ViewMode.getInstance().putAttr("doce", doce).putAttr("types", types)
                .returnModelAndView(ThemesUtil.getThemePath() + "/know/edit");
    }

    /**
     * 提交创建知识
     *
     * @return
     */
    @RequestMapping("/addsubmit")
    public ModelAndView submitAdd(String docgroup, String knowtitle, String knowtype, String text, String knowtag,
                                  String writetype, String readtype, HttpSession session) {
        DocEntire doc = null;
        try {
            if ("0".equals(docgroup)) {
                docgroup = null;
            }
            doc = KnowServiceImpl.creatKnow(knowtitle, knowtype, text, knowtag, DocOperateRightInter.POP_TYPE.getEnum(writetype),
                    DocOperateRightInter.POP_TYPE.getEnum(readtype), docgroup, getCurrentUser(session));
        } catch (Exception e) {
            return ViewMode.getInstance().setError(e.toString())
                    .returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }
        if (doc.getAudit() != null) {
            return ViewMode.getInstance().returnRedirectUrl("/audit/tempdoc.do?auditid=" + doc.getAudit().getId());
        }
        return ViewMode.getInstance().returnRedirectUrl("/webdoc/view/Pub" + doc.getDoc().getId() + ".html");
    }

    /**
     * 提交修改知识
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    @RequestMapping("/editsubmit")
    public ModelAndView editCommit(String docid, String docgroup, String knowtitle, String knowtype, String text,
                                   String knowtag, String writetype, String readtype, String editNote, HttpSession session) {
        DocEntire doc = null;
        try {
            if ("0".equals(docgroup)) {
                docgroup = null;
            }
            // 高级权限用户修改
            if (farmDocOperateRightImpl.isDel(getCurrentUser(session), farmDocManagerImpl.getDocOnlyBean(docid))) {
                doc = KnowServiceImpl.editKnow(docid, knowtitle, knowtype, text, knowtag, DocOperateRightInter.POP_TYPE.getEnum(writetype),
                        DocOperateRightInter.POP_TYPE.getEnum(readtype), docgroup, getCurrentUser(session), editNote);
                return ViewMode.getInstance().returnRedirectUrl("/webdoc/view/Pub" + docid + ".html");
            }
            // 低级权限用户修改
            {
                doc = KnowServiceImpl.editKnow(docid, text, knowtag, getCurrentUser(session), editNote);
            }
        } catch (Exception e) {
            List<TypeBrief> types = farmDocTypeManagerImpl.getPubTypesForWriteDoc(getCurrentUser(session));
            return ViewMode.getInstance().putAttr("doce", farmDocManagerImpl.getDoc(doc.getDoc().getId()))
                    .putAttr("types", types).returnModelAndView(ThemesUtil.getThemePath() + "/know/edit");
        }
        if (doc.getAudit() != null) {
            return ViewMode.getInstance().returnRedirectUrl("/audit/tempdoc.do?auditid=" + doc.getAudit().getId());
        }
        return ViewMode.getInstance().returnRedirectUrl("/webdoc/view/Pub" + doc.getDoc().getId() + ".html");
    }
}
